using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;

namespace GalaxyDefenderChaos
{
    class EndGameScene: VisibleGameEntity
    {
        List<BackToMainMenuListener> _Listeners = new List<BackToMainMenuListener>();
        protected My2DButton[] _ButtonList;
        string[] _StrTextureButtons = {
                                    "YouWin",
                                    "YouLose",
                                    "Menu/Back"
                                };
        Vector2[] _ButtonPositions = { 
                                        new Vector2(180, 220),
                                        new Vector2(180, 320),
                                        new Vector2(180, 420),
                                     };

        bool _IsWin = false;

        public bool IsWin
        {
            get { return _IsWin; }
            set { _IsWin = value; }
        }

        public EndGameScene(ContentManager content, GraphicsDevice graphicsDevice)
        {
            _ButtonList = new My2DButton[_StrTextureButtons.Count()];
            for (int i = 0; i < _ButtonList.Count(); i++)
            {
                _ButtonList[i] = new My2DButton(content, _StrTextureButtons[i], _ButtonPositions[i], new Vector2(512, 45));
            }
        }

        public override void Update(GameTime gameTime, TouchCollection touchCollection)
        {
            if (touchCollection.Count > 0 && touchCollection[0].State == TouchLocationState.Pressed)
            {
                Vector2 touchPoint = new Vector2(touchCollection[0].Position.X, touchCollection[0].Position.Y);
                int index = GetSelectedIndex(touchPoint);
                switch (index)
                {
                    case 0:
                        break;
                    case 1:
                        break;
                    case 2:
                        NotifyBackToMainMenuEvent();
                        break;
                }
            }
        }

        private void NotifyBackToMainMenuEvent()
        {
            for (int i = 0; i < _Listeners.Count; i++)
            {
                _Listeners[i].BackToMainMenuSelected();
            }
        }

        public void AddListener(BackToMainMenuListener listener)
        {
            _Listeners.Add(listener);
        }

        public override void Draw(GraphicsDevice graphicsDevice, Camera camera, GameTime gameTime)
        {
            if (IsWin)
            {
                _ButtonList[0].Draw(graphicsDevice, camera, gameTime);
            }
            else 
            {
                _ButtonList[1].Draw(graphicsDevice, camera, gameTime);
            }
            _ButtonList[2].Draw(graphicsDevice, camera, gameTime);
        }

        public int GetSelectedIndex(Vector2 point)
        {
            for (int i = 0; i < _ButtonList.Count(); i++)
            {
                if (_ButtonList[i].CheckInside(point))
                {
                    return i;
                }
            }
            return -1;
        }
    }
}
